package com.application.lucky.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.application.lucky.Lucky;


/**
 * Created by hemeng on 2017/11/27.
 */

public class NetWorkUtil {


    public static boolean available() {
        ConnectivityManager manager = (ConnectivityManager) Lucky.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
        // 下面这种方式，在 s6 上面会有 netid 501 的情况

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            if (manager != null) {
//                Network[] infos = manager.getAllNetworks();
//                if (infos == null || infos.length < 1) {
//                    return false;
//                } else {
//                    return true;
//                }
//            }
//            return false;
//        } else

            {
            if (manager != null) {
                NetworkInfo networkInfo = manager.getActiveNetworkInfo();
                if (networkInfo != null) {
                    return networkInfo.isConnectedOrConnecting();
                }
            }
            return false;
        }
    }


    public static boolean wifiConnected() {
        ConnectivityManager manager = (ConnectivityManager) Lucky.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return networkInfo.isAvailable();
        }
        return false;
    }


}
